package com.yic.module.device.job.inspectionPlan;

import cn.hutool.core.collection.CollectionUtil;
import com.yic.framework.quartz.core.handler.JobHandler;
import com.yic.framework.tenant.core.job.TenantJob;
import com.yic.module.device.controller.admin.inspectionDeviceEntry.vo.InspectionDeviceEntryExportReqVO;
import com.yic.module.device.controller.admin.inspectionPlanJob.vo.InspectionPlanJobExportReqVO;
import com.yic.module.device.controller.admin.inspectionRecord.vo.InspectionRecordCreateReqVO;
import com.yic.module.device.controller.admin.inspectionRecordInfo.vo.InspectionRecordInfoCreateReqVO;
import com.yic.module.device.controller.admin.maintenanceDeviceEntry.vo.MaintenanceDeviceEntryExportReqVO;
import com.yic.module.device.dal.dataobject.inspectionDeviceEntry.InspectionDeviceEntryDO;
import com.yic.module.device.dal.dataobject.inspectionPlan.InspectionPlanDO;
import com.yic.module.device.dal.dataobject.inspectionPlanDevice.InspectionPlanDeviceDO;
import com.yic.module.device.dal.dataobject.inspectionPlanEntry.InspectionPlanEntryDO;
import com.yic.module.device.dal.dataobject.inspectionPlanJob.InspectionPlanJobDO;
import com.yic.module.device.dal.dataobject.maintenanceDeviceEntry.MaintenanceDeviceEntryDO;
import com.yic.module.device.dal.dataobject.maintenancePlanEntry.MaintenancePlanEntryDO;
import com.yic.module.device.enums.inspectionPlanDevice.CommonStatusEnum;
import com.yic.module.device.enums.inspectionPlanDevice.DeviceInspectionPlanType;
import com.yic.module.device.enums.inspectionPlanDevice.InspectionTypeEnum;
import com.yic.module.device.service.inspectionDeviceEntry.InspectionDeviceEntryService;
import com.yic.module.device.service.inspectionPlan.InspectionPlanService;
import com.yic.module.device.service.inspectionPlanDevice.InspectionPlanDeviceService;
import com.yic.module.device.service.inspectionPlanEntry.InspectionPlanEntryService;
import com.yic.module.device.service.inspectionPlanJob.InspectionPlanJobService;
import com.yic.module.device.service.inspectionRecord.InspectionRecordService;
import com.yic.module.device.service.inspectionRecordInfo.InspectionRecordInfoService;
import com.yic.module.device.service.maintenanceDeviceEntry.MaintenanceDeviceEntryService;
import com.yic.module.device.service.maintenancePlanEntry.MaintenancePlanEntryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 巡检任务处理逻辑
 * @Author liuzhi
 * @Date 2023/12/18
 * @Version 1.0
 */
@Component
@Slf4j
public class InspectionPlanJob implements JobHandler {
    @Resource
    private InspectionPlanService inspectionPlanService;
    @Resource
    private InspectionPlanJobService inspectionPlanJobService;
    @Resource
    private InspectionPlanDeviceService inspectionPlanDeviceService;
    @Resource
    private InspectionDeviceEntryService inspectionDeviceEntryService;
    @Resource
    private InspectionRecordService inspectionRecordService;
    @Resource
    private InspectionPlanEntryService inspectionPlanEntryService;
    @Resource
    private InspectionRecordInfoService inspectionRecordInfoService;
    @Resource
    private MaintenancePlanEntryService maintenancePlanEntryService;
    @Resource
    private MaintenanceDeviceEntryService maintenanceDeviceEntryService;

    @Override
    @TenantJob
    public String execute(String param) throws Exception {
        Long planId = Long.valueOf(param);
        // 获取巡检任务计划信息
        InspectionPlanJobExportReqVO planJobExportReqVO = new InspectionPlanJobExportReqVO();
        planJobExportReqVO.setPlanId(planId);
        planJobExportReqVO.setStatus(CommonStatusEnum.ENABLE.getCode());
        List<InspectionPlanJobDO> inspectionPlanJobList = inspectionPlanJobService.getInspectionPlanJobList(planJobExportReqVO);
        // 存在有效巡检计划任务时执行逻辑
        if (CollectionUtil.isNotEmpty(inspectionPlanJobList)) {
            Long jobId = Long.valueOf(inspectionPlanJobList.get(0).getJobId());
            InspectionPlanDO inspectionPlan = inspectionPlanService.getInspectionPlan(planId);
            // 当在开始日期和结束日期范围内时执行逻辑
            if (!LocalDateTime.now().isBefore(inspectionPlan.getStartDate())
                    && !LocalDateTime.now().isAfter(inspectionPlan.getEndDate())) {
                // 获取巡检计划关联的设备信息
                List<InspectionPlanDeviceDO> inspectionPlanDeviceDOList = inspectionPlanDeviceService.selectListByPlanId(planId);
                for (InspectionPlanDeviceDO planDeviceDO : inspectionPlanDeviceDOList) {
                    Long deviceId = planDeviceDO.getDeviceId();
                    // 初始化巡检记录数据
                    InspectionRecordCreateReqVO createReqVO = new InspectionRecordCreateReqVO();
                    createReqVO.setCode(String.valueOf(new Date().getTime()));
                    // 1.巡检任务
                    if (inspectionPlan.getType().equals(DeviceInspectionPlanType.INSPECTION_PLAN.getCode())) {
                        createReqVO.setInspectionType(InspectionTypeEnum.INSPECTION.getCode());
                    } else if (inspectionPlan.getType().equals(DeviceInspectionPlanType.MAINTENANCE_PLAN.getCode())) {
                        // 2.保养任务
                        createReqVO.setInspectionType(InspectionTypeEnum.MAINTENANCE.getCode());
                    }
                    createReqVO.setPlanId(planId);
                    createReqVO.setDeviceId(deviceId);
                    createReqVO.setJobId(jobId);
                    Long inspectionRecordId = inspectionRecordService.createInspectionRecord(createReqVO);

                    List<Long> effectEntryIdList = new ArrayList<>();
                    if (inspectionPlan.getType().equals(DeviceInspectionPlanType.INSPECTION_PLAN.getCode())) {
                        // 初始化巡检记录明细数据
                        InspectionDeviceEntryExportReqVO deviceEntryExportReqVO = new InspectionDeviceEntryExportReqVO();
                        deviceEntryExportReqVO.setDeviceId(deviceId);
                        List<InspectionDeviceEntryDO> inspectionDeviceEntryList = inspectionDeviceEntryService.getInspectionDeviceEntryList(deviceEntryExportReqVO);
                        if (CollectionUtil.isNotEmpty(inspectionDeviceEntryList)) {
                            List<Long> entryIdList = inspectionDeviceEntryList.stream().map(InspectionDeviceEntryDO::getEntryId).collect(Collectors.toList());
                            List<InspectionPlanEntryDO> inspectionPlanEntryList = inspectionPlanEntryService.getInspectionPlanEntryList(entryIdList);
                            effectEntryIdList = inspectionPlanEntryList.stream()
                                    .filter(entry -> entry.getStatus().equals(CommonStatusEnum.ENABLE.getCode()))
                                    .map(InspectionPlanEntryDO::getId).collect(Collectors.toList());
                        }
                    } else if (inspectionPlan.getType().equals(DeviceInspectionPlanType.MAINTENANCE_PLAN.getCode())) {
                        // 初始化保养记录明细数据
                        MaintenanceDeviceEntryExportReqVO reqVO = new MaintenanceDeviceEntryExportReqVO();
                        reqVO.setDeviceId(deviceId);
                        List<MaintenanceDeviceEntryDO> maintenanceDeviceEntryList = maintenanceDeviceEntryService.getMaintenanceDeviceEntryList(reqVO);
                        if(CollectionUtil.isNotEmpty(maintenanceDeviceEntryList)){
                            List<Long> entryIdList = maintenanceDeviceEntryList.stream().map(MaintenanceDeviceEntryDO::getEntryId).collect(Collectors.toList());
                            List<MaintenancePlanEntryDO> maintenancePlanEntryList = maintenancePlanEntryService.getMaintenancePlanEntryList(entryIdList);
                            effectEntryIdList = maintenancePlanEntryList.stream()
                                    .filter(entry -> entry.getStatus().equals(CommonStatusEnum.ENABLE.getCode()))
                                    .map(MaintenancePlanEntryDO::getId).collect(Collectors.toList());
                        }
                    }
                    if(CollectionUtil.isNotEmpty(effectEntryIdList)){
                        for (Long entryId : effectEntryIdList) {
                            InspectionRecordInfoCreateReqVO createReqVO1 = new InspectionRecordInfoCreateReqVO();
                            createReqVO1.setRecordId(inspectionRecordId);
                            createReqVO1.setPlanId(planId);
                            createReqVO1.setDeviceId(deviceId);
                            createReqVO1.setJobId(jobId);
                            createReqVO1.setEntryId(entryId);
                            inspectionRecordInfoService.createInspectionRecordInfo(createReqVO1);
                        }
                    }
                }
            }
        }
        return "";
    }
}
